package com.xingdata.jjxc.m.login.avt;

import android.content.ContentResolver;
import android.content.Intent;
import android.content.Intent.ShortcutIconResource;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.Parcelable;
import android.util.Log;

import com.ab.http.AbRequestParams;
import com.ab.http.AbStringHttpResponseListener;
import com.alibaba.fastjson.JSON;
import com.xingdata.jjxc.App;
import com.xingdata.jjxc.Common;
import com.xingdata.jjxc.R;
import com.xingdata.jjxc.base.BaseActivity;
import com.xingdata.jjxc.enty.RespEntity;
import com.xingdata.jjxc.m.avt.Aboutavt;
import com.xingdata.jjxc.m.avt.Mainavt;
import com.xingdata.jjxc.utils.Code;
import com.xingdata.jjxc.utils.ExitApplication;
import com.xingdata.jjxc.utils.SP;
import com.xingdata.jjxc.utils.SharedConfig;
import com.xingdata.jjxc.utils.SharedPreTools;
import com.xingdata.jjxc.utils.SystemInfo;
import com.xingdata.jjxc.views.Widget;

public class Splash extends BaseActivity {
	/**
	 * 判断是否第一次登录
	 */
	private boolean first;
	/**
	 * 
	 * Shared新建
	 */
	private SharedPreferences shared;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// setContentView(R.layout.activity_splash);
//		ExitApplication.getInstance().addActivity(this);
		SystemInfo.setUserInfo(SP.getUserInfo(Splash.this));
		Boolean b = !isAddShortCut();
		Boolean b1 = SP.getCreateLogo(this).equals("");
		Log.i("createLogo", b + "=" + b1 + "=" + SP.getCreateLogo(this) + "=");
		if ((!isAddShortCut()) && (SP.getCreateLogo(this).equals(""))) {
			SP.saveCreateLogo(this);
			addShortCut();
		}
		// 得到配置文件
		shared = new SharedConfig(this).GetConfig();
		first = shared.getBoolean("First", true);
		//
		if (SystemInfo.isNetworkConnected(this)) {
//			doPost_checkVersion(App.ZZD_REQUEST_CHECKVERSION);
		} else {
//			welcome();
			nextController(new Intent(Splash.this, Mainavt.class));
			showToast("请检查网络");
			
		}

	}

//	private void doPost_checkVersion(String url) {
//		if (isNetworkConnected() == false) {
//			showToast("网络连接断开，请检查网络");
//			return;
//		}
//		resp = null;
//
//		params = new AbRequestParams();
//
//		params.put("access_token", Code.accessToken());
//		String strVersionName = "22"
//				+ SystemInfo.getAPKVersionName(this).replace(".", "");
//		params.put("version", strVersionName);
//
//		mAbHttpUtil.post(url, params, new AbStringHttpResponseListener() {
//			@Override
//			public void onSuccess(int statusCode, String content) {
//				resp = JSON.parseObject(content, RespEntity.class);
//			}
//
//			@Override
//			public void onFailure(int statusCode, String content,
//					Throwable error) {
//				
//			}
//
//			@Override
//			public void onStart() {
//			}
//
//			@Override
//			public void onFinish() {
//				if (resp != null) {
//					String msg = resp.getMsg();
//					// 等于1提示升级
//					if (resp.getState() != 1) {
//						Widget.checkVersion(Splash.this, resp.getState() + "",
//								App.ZZD_VERSION_UPGRADE);// 差版本升级链接App.ZZD_VERSION_UPGRADE
//					} else {
//						Intent intent;
//						if (first) {
//							intent = new Intent(Splash.this, Welcom.class);
////							initShardValue();
//						} else {
//							intent = new Intent(Splash.this, Mainavt.class);
//						}
//						startActivity(intent);
//						Widget.startActivityAnim(Splash.this, R.anim.fade_in,
//								R.anim.fade_out);
//						finish();
//					}
//				} else {
//					showToast("请检查网络");
//				}
//				Intent intent = new Intent();
//				nextController(new Intent(Splash.this, Mainavt.class));
////					finish();
////					return;
//			}
//		});
//
//	}

	private void welcome() {
		// first = shared.getBoolean("First", true);
		new CountDownTimer(2000, 1000) {
			@Override
			public void onTick(long millisUntilFinished) {
			}

			@Override
			public void onFinish() {
				Intent intent;
				if (first) {
					intent = new Intent(Splash.this, Welcom.class);
//					initShardValue();
				} else {
					intent = new Intent(Splash.this, Mainavt.class);
				}
				nextController(intent);
			}

		}.start();
	}

	public boolean isAddShortCut() {

		boolean isInstallShortcut = false;
		final ContentResolver cr = this.getContentResolver();

		int versionLevel = android.os.Build.VERSION.SDK_INT;
		String AUTHORITY = "com.android.launcher2.settings";

		// 2.2以上的系统的文件文件名字是不一样的
		if (versionLevel >= 8) {
			AUTHORITY = "com.android.launcher2.settings";
		} else {
			AUTHORITY = "com.android.launcher.settings";
		}
		final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY
				+ "/favorites?notify=true");
		Cursor c = cr.query(CONTENT_URI,
				new String[] { "title", "iconResource" }, "title=?",
				new String[] { getString(R.string.app_name) }, null);
		if (c != null && c.getCount() > 0) {
			isInstallShortcut = true;
		}
		return isInstallShortcut;
	}

	// 添加快捷方式：
	public void addShortCut() {

		Intent shortcut = new Intent(
				"com.android.launcher.action.INSTALL_SHORTCUT");
		// 设置属性
		shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME,
				getResources().getString(R.string.app_name));
		ShortcutIconResource iconRes = Intent.ShortcutIconResource.fromContext(
				this.getApplicationContext(), R.drawable.ic_launcher);
		shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON, iconRes);

		// 是否允许重复创建
		shortcut.putExtra("duplicate", false);

		// 设置桌面快捷方式的图标
		Parcelable icon = Intent.ShortcutIconResource.fromContext(this,
				R.drawable.ic_launcher);
		shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon);

		// 点击快捷方式的操作
		Intent intent = new Intent(Intent.ACTION_MAIN);
		intent.setFlags(Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
		intent.addFlags(Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY);
		intent.addCategory(Intent.CATEGORY_LAUNCHER);
		intent.setClass(this, Splash.class);
		// 设置启动程序
		shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, intent);

		// 广播通知桌面去创建
		this.sendBroadcast(shortcut);
	}

//	/**
//	 * 初始化各种开关
//	 */
//	private void initShardValue() {
//		SharedPreTools.putString(this, Common.BRIGHTNESS_SWITCH, "5");
//		SharedPreTools.putString(this, Common.CALLUP_SWITCH, "0");
//		SharedPreTools.putString(this, Common.MMS_SWITCH, "0");
//		SharedPreTools.putString(this, Common.QQ_SWITCH, "0");
//		SharedPreTools.putString(this, Common.WeiXin_SWITCH, "0");
//		SharedPreTools.putString(this, Common.WangXin_SWITCH, "0");
//		SharedPreTools.putString(this, Common.TTS_SWITCH, "0");
//
//	}

	@Override
	protected void initViews() {

	}

	@Override
	protected int getContentViewId() {
		return R.layout.activity_splash;
	}

	@Override
	protected String getTitleText() {
		return null;
	}
	
	private void nextController(Intent intent) {
		startActivity(intent);
		Widget.startActivityAnim(Splash.this, R.anim.fade_in,
				R.anim.fade_out);
		finish();
	}

}
